﻿using Shop.Model.AdvertPlace;
using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.AdvertPlace.Model;
using Shop.Repository;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Collect.lmpl
{
    internal class AdvertPlaceCollect : IAdvertPlaceCollect
    {
        private readonly IAdvertPlaceRepository _Repository;

        public AdvertPlaceCollect ( IAdvertPlaceRepository repository )
        {
            this._Repository = repository;
        }
        public DBAdvertPlace Get ( long id )
        {
            return this._Repository.Get(id);
        }
        public void Delete ( DBAdvertPlace place )
        {
            if ( place.PlaceStatus != AdvertPlaceStatus.起草 )
            {
                throw new ErrorException("shop.advert.place.not.allow.delete");
            }
            this._Repository.Delete(place.Id);
        }
        public Result[] Gets<Result> ( AdvertPlaceGetParam query ) where Result : class, new()
        {
            return this._Repository.Gets<Result>(query);
        }
        public Dictionary<long, string> GetExampleImg ( string[] placeType )
        {
            var datas = this._Repository.Gets(a => placeType.Contains(a.PlaceType) && a.PlaceStatus == AdvertPlaceStatus.启用, a => new
            {
                a.Id,
                a.ExampleImg
            });
            return datas.Where(c => c.ExampleImg.IsNotNull()).ToDictionary(a => a.Id, a => a.ExampleImg);
        }
        public Result[] Gets<Result> ( AdvertPlaceStatus? status ) where Result : class, new()
        {
            if ( status.HasValue )
            {
                return this._Repository.Gets<Result>(a => a.PlaceStatus == status.Value);
            }
            return this._Repository.GetAll<Result>();
        }
        public long Add ( PlaceAdd add )
        {
            if ( this._Repository.IsExist(c => c.PlaceTitle == add.PlaceTitle) )
            {
                throw new ErrorException("shop.advert.place.title.repeat");
            }
            return this._Repository.Add(add);
        }
        public Result[] Query<Result> ( AdvertPlaceQueryParam query, IBasicPage paging, out int count ) where Result : class
        {
            return this._Repository.Query<Result>(query, paging, out count);
        }
        public bool Set ( DBAdvertPlace place, PlaceSet set )
        {
            if ( place.PlaceTitle != set.PlaceTitle && this._Repository.IsExist(c => c.PlaceTitle == set.PlaceTitle) )
            {
                throw new ErrorException("shop.advert.place.title.repeat");
            }
            return this._Repository.Update(place, set);
        }
        public T Get<T> ( long id ) where T : class
        {
            return this._Repository.Get<T>(id);
        }
        public Dictionary<long, string> GetNames ( long[] ids )
        {
            return this._Repository.Gets(ids, c => new
            {
                c.Id,
                c.PlaceTitle
            }).ToDictionary(c => c.Id, c => c.PlaceTitle);
        }
        public void SetStatus ( DBAdvertPlace place, AdvertPlaceStatus status )
        {
            if ( place.PlaceStatus == status )
            {
                return;
            }
            this._Repository.SetStatus(place, status);
        }
    }
}
